﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using DAL;

namespace CTRL
{
    public sealed class CtrlBatteryManufacturer
    {
        private static volatile CtrlBatteryManufacturer instance;
        private static object syncRoot = new Object();

        private IDALBatteryManufacturer bManRepo;

        public CtrlBatteryManufacturer()
        {
            this.bManRepo = new DALBatteryManufacturer(new CarBatteryEntities());
        }

        public static CtrlBatteryManufacturer Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncRoot)
                    {
                        if (instance == null)
                            instance = new CtrlBatteryManufacturer();
                    }
                }
                return instance;
            }
        }

        public IEnumerable<BatteryManufacturer> GetAllBatteryManufacturers()
        {
            return bManRepo.GetBatteryManufacturers();
        }

        public BatteryManufacturer GetBatteryManufacturer(int bManID)
        {
            return bManRepo.GetBatteryManufacturerByID(bManID);
        }

        public BatteryManufacturer GetBatteryManufacturerByName(string bManName)
        {
            return bManRepo.GetBatteryManufacturerByName(bManName);
        }

        public int CreateBatteryManufacturer(string name)
        {
            bManRepo.InsertBatteryManufacturer(new BatteryManufacturer() { name = name });

            return bManRepo.Save();
        }

        public int UpdateBatteryManufacturer(int manufacturerID, string name)
        {
            BatteryManufacturer bMan = GetBatteryManufacturer(manufacturerID);

            if (bMan != null)
            {
                bMan.name = name;
                bManRepo.UpdateBatteryManufacturer(bMan);
            }

            return bManRepo.Save();
        }

        public int DeleteBatteryManufacturer(int manufacturerID)
        {
            BatteryManufacturer bMan = GetBatteryManufacturer(manufacturerID);

            if (bMan != null)
                bManRepo.DeleteBatteryManufacturer(manufacturerID);

            return bManRepo.Save();
        }
    }
}